#! /bin/bash

function parallelTest(){

    echo "#! /bin/bash
    dir=\$(pwd)
    cd 'data-t3/'
    for i in *
    do
        echo \$i
        mpirun --hostfile ../hosts -np \$1 ../Parallel \$i
        echo
        echo
    done
    cd \$dir" > parallelTest.sh
    chmod +x parallelTest.sh
}

function serialTest(){
    echo "#! /bin/bash
    dir=\$(pwd)
    cd 'data-t3/' 
    for i in *
    do
        echo \$i
        ../Serial \$i
        echo
        echo
    done
    cd \$dir" > serialTest.sh
    chmod +x serialTest.sh
}

if [ ! -d "data-t3" ]; then
    wget http://lasdpc.icmc.usp.br:9191/~edwin/pc/data-t3.zip
    unzip data-t3.zip
    rm data-t3.zip
fi

if [ ! -d "Results" ]; then
    mkdir Results
fi

case $1 in

    Serial)
        serialTest
        out_file='serialResults.txt'
        nohup ./serialTest.sh > $out_file
        rm serialTest.sh
    ;;
    Parallel)
        
            parallelTest
            for ((i=($2); i<=($3); i++))
            do
                out_file='parallel'$i'Result.txt'
                echo $out_file
                nohup ./parallelTest.sh $i > $out_file 2>/dev/null
            done
            rm parallelTest.sh
    ;;
    Full)
        serialTest
        parallelTest

        out_file='serialResults.txt'
        nohup ./serialTest.sh  > $out_file

        for ((i=($2); i<=($3); i++))
        do
            out_file='parallel'$i'Result.txt'
            echo $out_file
            nohup ./parallelTest.sh $i > $out_file 2>/dev/null
        done

        rm parallelTest.sh
        rm serialTest.sh
    ;;
esac

mv *.txt Results/
